/* Zatok - язык программирования текстовых игр
 * (С) 2010 - 2012 Меркулов Матвей Юрьевич
 * Код лицензирован под лицензией GNU LGPL
 * Вы можете прочитать текст лицензии по адресу:
 * http://www.gnu.org/copyleft/lesser.html
 * или прочитать русский перевод по адресу:
 * http://ru.wikisource.org/wiki/GNU_Lesser_General_Public_License
 */

package base;

import base.intrface.CCommandText;

public class CGUI {
	public static CWindow DescriptionWindow = new CWindow();
	public static CWindow InformationWindow = new CWindow();
	public static CWindow CurrentOutputWindow;
	public static int[] Margins = new int[ 4 ];
	public static int[] MenuMargins = new int[ 4 ];
	public static CCommandText GadgetUnderMouse;
	public static double MaxImageHeight = 0.3;
	public static double MaxDescriptionHeight = 0.7;
	public static int ImageWidth, ImageHeight, TotalImageHeight;
	public static int ImageMargin = 16;
	public static int WindowsMargin = 16;
	public static int MaxWidth = 0, MaxHeight;
	
	public static int StartingYShift, StartingY;
	public static CWindow DraggingSliderWindow;
	public static boolean FixedWindowSize;



	public static void Rearrange( boolean DescriptionOnly ) {
		CGraphics.WaitToCompleteDrawing();
		
		DescriptionWindow.Rearrange();
		if( !DescriptionOnly ) InformationWindow.Rearrange();

		UpdateImageSize();

		DescriptionWindow.X = Margins[ 0 ];
		DescriptionWindow.Y = Margins[ 1 ] + TotalImageHeight;

		int CanvasHeight = CGraphics.GetCanvasHeight() - Margins[ 1 ] - Margins[ 3 ];
		int MaxHeight = (int) Math.floor( 0.6 * CanvasHeight );
		if( TotalImageHeight + DescriptionWindow.Height > MaxHeight ) {
			DescriptionWindow.ViewHeight = MaxHeight - TotalImageHeight;
		} else {
			DescriptionWindow.ViewHeight = DescriptionWindow.Height;
		}
		
		int TotalDescriptionHeight = TotalImageHeight + DescriptionWindow.ViewHeight + WindowsMargin;
		
		InformationWindow.X = Margins[ 0 ];
		InformationWindow.Y = Margins[ 1 ] + TotalDescriptionHeight;
		InformationWindow.ViewHeight = CanvasHeight - TotalDescriptionHeight;
		
		UpdateImageSize();
		
		CGraphics.Repaint();
	}



	public static void UpdateImageSize() {
		if ( CGraphics.DrawingImage != null ) {
			int RealImageWidth = CGraphics.GetBaseImageWidth();
			int RealImageHeight = CGraphics.GetBaseImageHeight();
			double ImageScale = Math.min( 1.0, ( CGraphics.GetCanvasHeight() - Margins[ 2 ] - Margins[ 3 ] ) * MaxImageHeight / RealImageHeight );
			ImageScale = Math.min( ImageScale, 1.0 * ( CGraphics.GetCanvasWidth() - Margins[ 0 ] - Margins[ 1 ] ) / RealImageWidth );
			ImageWidth = ( int ) ( ImageScale * RealImageWidth );
			ImageHeight = ( int ) ( ImageScale * RealImageHeight );
			TotalImageHeight =ImageHeight + ImageMargin;
		} else {
			TotalImageHeight =0;
		}
	}
}
